//
//  MyFundListViewController.m
//  Umoney
//
//  Created by zwz on 16/8/31.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "MyFundListViewController.h"
#import "MyFundListTableViewCell.h"
#import "MyFundDetailViewController.h"

@interface MyFundListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_fundListTableView;
    NSString *UID;
    NSArray *res;
}

@end

@implementation MyFundListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    UID = [user objectForKey:@"uid"];
    
    [self CreateTableView];
    
    [self Getdata];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)CreateTableView
{
    _fundListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _fundListTableView.delegate = self;
    _fundListTableView.dataSource = self;
    [self.view addSubview:_fundListTableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [res count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"MyFundListTableViewCell";
    MyFundListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor = nil;
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellID owner:self options:nil] lastObject];
    }
    

    cell.ProductName.text = [res[indexPath.row] valueForKey:@"pname"];
    cell.Code.text = [res[indexPath.row] valueForKey:@"pid"];
    
    NSString *PricipalMoney = [res[indexPath.row] valueForKey:@"pricipalmoney"];
    NSString *InterestMoney = [res[indexPath.row] valueForKey:@"interestmoney"];
    float total = [PricipalMoney floatValue] + [InterestMoney floatValue];
    
    cell.DetailMoney.text = [NSString stringWithFormat:@"%.2f",total];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //得到详细fund数据载入。。。
    MyFundListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    MyFundDetailViewController *detail = [[MyFundDetailViewController alloc] initWithCode:cell.Code.text andUser:UID];
    
    while (![detail.ready isEqualToString:@"ready"]) {
    }
    
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)Getdata
{
    NSString *urlString = [NSString stringWithFormat:@"http://23.106.134.160/umoney/search_myinvest.php?uid=%@",UID];
    
    NSLog(@"%@",urlString);
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    config.timeoutIntervalForRequest = 30.0f;
    config.timeoutIntervalForResource = 60.0f;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        res = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        
        [_fundListTableView reloadData];
        
    }];
    [task resume];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
